﻿Imports System.IO

Public Class frmMain
    Private ldsMeetings As New dsMeetings
    Dim ldrMeeting As dsMeetings.MeetingsRow
    Private lDataFile As String = "MeetingMate_Data.xml"

    Sub BindData()
        lbPersons.DataSource = ldsMeetings.Persons
        lbPersons.DisplayMember = "Name"
        lbPersons.ValueMember = "PersonID"

        lbAttendees.DataSource = ldsMeetings.Attendees
        lbAttendees.DisplayMember = "Name"
        lbAttendees.ValueMember = "PersonID"

        txtNewPerson.DataBindings.Add(New Binding("Text", ldsMeetings.Meetings, "Title"))
        dtMeetingDate.DataBindings.Add(New Binding("Value", ldsMeetings.Meetings, "MeetingDate"))
    End Sub

#Region "Data IO ***********************************************************"
    Sub SaveData()
        ldsMeetings.WriteXml(lDataFile)
    End Sub
    Sub LoadData()
        If File.Exists(lDataFile) Then
            ldsMeetings.ReadXml(lDataFile)
        Else
            SaveData()
        End If
    End Sub
#End Region

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
        BindData()
    End Sub

    Private Sub NewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripButton.Click
        ldrMeeting = ldsMeetings.Meetings.NewMeetingsRow()
        ldsMeetings.Meetings.AddMeetingsRow(ldrMeeting)
    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        SaveData()
    End Sub

    Private Sub btnAddNewPerson_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNewPerson.Click

        Dim dr As dsMeetings.PersonsRow = ldsMeetings.Persons.NewRow
        dr.Name = txtNewPerson.Text
        ldsMeetings.Persons.AddPersonsRow(dr)
        lbPersons.Refresh()

        Dim drAttendees As dsMeetings.AttendeesRow = ldsMeetings.Attendees.NewRow
        drAttendees.Name = txtNewPerson.Text
        drAttendees.PersonID = dr.PersonID
        ldsMeetings.Attendees.AddAttendeesRow(drAttendees)
        lbAttendees.Refresh()

    End Sub

    Private Sub btnAddPerson_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPerson.Click
        If IsNothing(ldsMeetings.Attendees.FindByPersonID(lbPersons.SelectedValue)) Then
            Dim dr As dsMeetings.AttendeesRow = ldsMeetings.Attendees.NewRow
            dr.Name = lbPersons.Text
            dr.PersonID = lbPersons.SelectedValue
            ldsMeetings.Attendees.AddAttendeesRow(dr)
            lbAttendees.Refresh()
        End If
    End Sub

    Private Sub btnRemovePerson_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemovePerson.Click
        Dim dr As dsMeetings.AttendeesRow = ldsMeetings.Attendees.FindByPersonID(lbAttendees.SelectedValue)
        dr.Delete()
        lbAttendees.Refresh()
    End Sub

    Private Sub HelpToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripButton.Click

    End Sub
End Class
